package com.ai.chatai.tools;

import com.ai.chatai.entity.po.Course;
import com.ai.chatai.entity.po.CourseReservation;
import com.ai.chatai.entity.po.School;
import com.ai.chatai.entity.query.CourseQuery;
import com.ai.chatai.service.ICourseReservationService;
import com.ai.chatai.service.ICourseService;
import com.ai.chatai.service.ISchoolService;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
public class CourseTools {
    private final ICourseService courseService;
    private final ISchoolService schoolService;
    private final ICourseReservationService courseReservationService;

    @Tool(description = "查询课程信息")
    public List<Course> queryCourses(@ToolParam(description = "查询的条件") CourseQuery query) {
        if(query == null){
            return courseService.list();
        }
        QueryChainWrapper<Course> wrapper = courseService.query()
                .eq(query.getType() != null, "type", query.getType())// 课程类型
                .eq(query.getEdu() != null, "edu", query.getEdu());// 学生年级要求
        if(query.getSorts() != null && !query.getSorts().isEmpty()){
            for (CourseQuery.Sort sort : query.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Tool(description = "查询学校信息")
    public List<School> querySchools(){
        return schoolService.list();
    }

    @Tool(description = "生成课程预约信息")
    public Integer createCourseReservations(
            @ToolParam(description = "预约的课程") String course,
            @ToolParam(description = "预约的学校") String school,
            @ToolParam(description = "学生姓名") String studentName,
            @ToolParam(description = "联系电话") String contactInfo,
            @ToolParam(description = "备注", required = false) String remark){
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(course);
        courseReservation.setSchool(school);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setRemark(remark);
        courseReservationService.save(courseReservation);//保存预约信息
        return courseReservation.getId();//返回预约id
    }
}
